/**
 * Created by Administrator on 2018/1/29.
 */
$(function () {
    //   动态设置左侧导航栏的右边线和右边内容长度相同
    var H = $('.order-content').height();
    $('.Order-List').height(H);

    //  My-Coupons 页面中判断有没有列表，展示不同的内容
    var con = $('.coupons-content').length;
    if(con == 0){
        $('.No-Count').css('display','block')
    }else {
        $('.No-Count').css('display','none')
    }

    //   模拟 checkbox 功能
    $('.Men-Right').on('click','.check',function () {
        var check = $(this).children().attr('class');
        if(check == 'checked'){
            $(this).children().attr('class','');
        }else{
            $(this).children().attr('class','checked');
        }
    });

    //  Message-Inbox  页面全选功能
    $('.Sele-Dele .check').click(function () {
        if($(this).children().attr('class') == 'checked'){
            $('.Msg-List .check').children('a').removeClass('checked');
            $('.total-buy').css('display','none');
        }else{
            $('.Msg-List .check').children('a').addClass('checked');
            $('.total-buy').css('display','block');
        }
    });

    //  Message-Inbox  页面删除选项功能
    $('.Dele').click(function () {
        var i = 0,
            j = 0,
            dataId = '';
        var content = $('.Msg-List .check');
        var checked = $('.Msg-List .checked');
        for(j;j<checked.length;j++){
            var str = '-' + $(checked[j]).parents('.Msg-Content').attr('data-id');
            dataId = dataId.concat(str);
        }
        if($('.checked').length > 0){
            $('.Delete-Fram').css('display','table');
            $('.Yes-Btn').click(function () {
                alert(dataId.substr(1,dataId.length-1));
                for(i; i<content.length; i++){
                    if($(content[i]).children().attr("class") == 'checked'){
                        $(this).parents('.Delete-Fram').css('display','none');
                        $(content[i]).parent().parent().remove();
                    }
                }
            });
        }
    });

//  Account-Information  页面编辑功能的打开
    $('.Per-Right').click(function () {
        var aa = $(this).prev().children('.Info-content').css('display');
        if( aa == 'block'){
            $(this).prev().children('.Info-content').css('display','none');
            $(this).prev().children('.Edit-Content').css('display','block');
            $(this).prev().prev().css('lineHeight','84px');
        }else{
            $(this).prev().children('.Info-content').css('display','block');
            $(this).prev().children('.Edit-Content').css('display','none');
            $(this).prev().prev().css('lineHeight','42px');
        }
    });

//  Account-Information  页面编辑功能的关闭
    $('.Info-Cancel').click(function () {
        $(this).parent().parent().css('display','none');
        $(this).parent().parent().prev().css('display','block');
        $(this).parents('.Per-Center').prev().css('lineHeight','42px');
    });
    $('.Info-Save').click(function () {
        var content = $(this).parent().prev().val();
        var mKey = $(this).parent().prev().attr('name');
        // alert(mKey)
        if(content == ''){
            alert('Please enter the content');
        }else{
            $(this).parent().parent().prev().html(content);
            $(this).parent().parent().css('display','none');
            $(this).parent().parent().prev().css('display','block');
            $(this).parents('.Per-Center').prev().css('lineHeight','42px');
            // $.get('member-account-ajax.php', {id:<?php echo $memberRow['m_id']; ?>, mkey:mKey, mval:content, m:Math.random()});
        }
    });
//    Account-Information  页面设置及存储密码
    $('.Pass-Save').click(function () {
        var Pasold = $('.pass-old').val();
        var Pasnew = $('.pass-new').val();
        var Pascfm = $('.pass-cfm').val();
        var passreg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,}$/;
        if(Pasold == ''){
            $('.pas-old-tip').children('span').html('password can not be blank!');
            $('.pas-old-tip').css('display','block');
        }else{
            $('.pas-old-tip').css('display','none');
        }
        if(Pasnew == ''){
            $('.pas-new-tip').children('span').html('password can not be blank!');
            $('.pas-new-tip').css('display','block');
        }else if(!passreg.test(Pasnew)){
            $('.pas-new-tip').children('span').html('Password must contain both letters and numbers,at least six characters.');
            $('.pas-new-tip').css('display','block');
        }else{
            $('.pas-new-tip').css('display','none');
        }
        if(Pascfm == ''){
            $('.pas-tip').children('span').html('password can not be blank!');
            $('.pas-tip').css('display','block');
        }else{
            $('.pas-tip').children('span').html('');
        }
        if(Pascfm != '' && Pascfm != Pasnew){
            $('.pas-tip').children('span').html("Your passwords do not match, please try again.");
            $('.pas-tip').css('display','block');
        }else{
            $('.pas-tip').css('display','none');
        }
        if(Pasold != '' && Pasnew != '' && Pascfm != '' && passreg.test(Pasnew) && Pascfm == Pasnew){
            var PassWord = $('.pas-tip').prev().val();
            console.log(PassWord);
            $(this).parent().parent().css('display','none');
            $(this).parent().parent().prev().css('display','block');
            $(this).parents('.Per-Center').prev().css('lineHeight','42px');
            $('.pass-success-tip').css('display','block');
            setTimeout(function () {
                $('.pass-success-tip').css('display','none')
            },1000);
            return true
        }else{
            return false
        }
    });
    //    页面刷新密码修改成功提示出现
    $(document).ready(function () {
        $('.pass-success-tip').css('display','block');
        setTimeout(function () {
            $('.pass-success-tip').css('display','none')
        },2000);
    });
//    Account-Information  页面旧密码失去焦点功能
    $('.pass-new').focus(function () {
        var val = $('.pass-old').val();
        console.log(val);
    });
    //  Character-Information-Add  页面调用 select2 插件
    $('.Cha-Game').select2();
    $('.Cha-Server').select2();
    $('.Cha-Plant').select2({
        minimumResultsForSearch: Infinity
    });

    //  Character-Information  页面点击删除会删除一组信息
    $('.char-dele').click(function () {
        $('.Delete-Fram').css('display','table');
        var dataid = $(this).parents('.Char-Main').prev().attr('data-id');
        var _this = this;
        $('.Yes-Btn').unbind('click').click(function () {
            alert(dataid);
            $(this).parents('.Delete-Fram').css('display','none');
            $(_this).parents('.Charscter-Group').remove();
        });
    });

    //  Character-Information  页面左上角显示选中状态
    $('.Char-Title').click(function () {
        $('.Char-Title a').removeClass('char-checkde');
        $(this).children('a').addClass('char-checkde');
        var DataId = $(this).attr('data-id');  //  获取所点击元素的data-id属性
        window.location.reload();   //  刷新当前页面
    });

    //  Unpaid  页面点击 Edit 数量可编辑
    $('.li-last-aF').click(function () {
        $('.Un-Count').css('display','none');
        $('.Edit-Un-Count').css('display','inline-block');
    });

    //  Unpaid  页面点击加减号改变文本框数据
    $('.Un-Reduce').click(function () {
        var count = $(this).next().val();
        var countPrice = $('.count-coupons').html();
        var totalPrice = $('.count-total').html();
        var Price = $(this).parents('li').next().find('.li-price').html();
        var newPrice;
        if(count > 1){
            count--;
            $(this).next().val(count);
            newPrice = parseFloat(Price);
            var newcoupons = parseFloat((parseFloat(countPrice) - newPrice*0.9)).toFixed(2);
            var newtotal = parseFloat((parseFloat(totalPrice) - newPrice)).toFixed(2);
            $('.count-coupons').html(newcoupons);
            $('.count-total').html(newtotal);
        }else{
            $(this).next().val(1);
        }
    });
    $('.Un-Add').click(function () {
        var count = $(this).prev().val();
        var countPrice = $('.count-coupons').html();
        var totalPrice = $('.count-total').html();
        var Price = $(this).parents('li').next().find('.li-price').html();
        var newPrice;
        count++;
        $(this).prev().val(count);
        newPrice = parseFloat(Price);
        var newcoupons = parseFloat((newPrice*0.9+parseFloat(countPrice))).toFixed(2);
        var newtotal = parseFloat((newPrice+parseFloat(totalPrice))).toFixed(2);
        $('.count-coupons').html(newcoupons);
        $('.count-total').html(newtotal);
    });

    //  My-Order  页面点击 delete 删除某条信息
    /*$('.li-last-aS').click(function () {
        $('.Delete-Fram').css('display','table');
        var _this = this;
        $('.Yes-Btn').click(function () {
            $(this).parents('.Delete-Fram').css('display','none');
            $(_this).parents('.list-content').remove();
        });
    });*/

    //  点击关闭删除提示的弹出框
    $('.Close-Dele').click(function () {
        $(this).parents('.Delete-Fram').css('display','none');
    });
    //   点击删除弹出框的Yes 和 No 按钮
    $('.No-Btn').click(function () {
        $(this).parents('.Delete-Fram').css('display','none');
    });

    //   发送成功弹出
    $('.Leave-Sub').click(function () {
        $('.Delete-Fram').css('display','table');
        /* leave message 页面提交成功后的提示设置1秒后自动消失 */
        setTimeout(function () {
            $('.Delete-Fram').css('display','none');
        }, 2500);
    });
    //  点击确定按钮关闭发送成功提示框
    $('.Success-Btn').click(function () {
        $('.Delete-Fram').css('display','none');
    });

    //  登录和注册页面的提交验证
    var reg =/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/;
    var passreg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,}$/;
    //  校验邮箱
    $('.EMail').blur(function () {
        var Email = $('.EMail').val();
        if(!reg.test(Email)){
            $('.EMail').next().css('visibility','visible').html("That doesn't look like a valid email.");
        }else{
            $('.EMail').next().css('visibility','hidden');
        }
    });
    //  校验密码
    $('.PassWord').blur(function () {
        var PWcontent = $('.PassWord').val();
        if(PWcontent == ''){
            $('.PassWord').next().html("Password must contain both letters and numbers,at least six characters");
            $('.PassWord').next().css('visibility','visible');
        }else if(!passreg.test(PWcontent)){
            $('.PassWord').next().html("Password must contain both letters and numbers,at least six characters");
            $('.PassWord').next().css('visibility','visible');
        }else{
            $('.PassWord').next().css('visibility','hidden');
        }
    });
    //  校验确认密码
    $('.CfmWord').blur(function () {
        var ComContent = $('.CfmWord').val();
        var PWcontent = $('.PassWord').val();
        if(ComContent != PWcontent){
            $('.CfmWord').next().html("Your passwords do not match, please try again.");
            $('.CfmWord').next().css('visibility','visible');
        }else{
            $('.CfmWord').next().css('visibility','hidden');
        }
    });
    //  点击注册提交按钮
    $('.register-btn').click(function () {
        var email = reg.test($('.EMail').val()),
            PWcontent = $('.PassWord').val(),
            cfmPassword = $('.CfmWord').val();
        if(email && PWcontent !='' && cfmPassword === PWcontent){
            return true
        }else{
            return false
        }
    });
    //  点击登录提交按钮
    $('.login-btn').click(function () {
        var email = reg.test($('.EMail').val()),
            PWcontent = $('.PassWord').val();
        if(email && PWcontent !=''){
            return true
        }else{
            return false
        }
    });
    /*$('.login-btn').click(function () {
     var Email = $('.EMail').val();
     var PWcontent = $('.PassWord').val();
     var reg =/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/;
     var TorF = true;
     // alert(reg.test(Email));
     if(!reg.test(Email)){
     $('.EMail').next().css('visibility','visible').html('E-mail is not entered correctly.');
     TorF = false;
     }else{
     $('.EMail').next().css('visibility','hidden');
     TorF = true
     }
     if(PWcontent == ''){
     $('.PassWord').next().css('visibility','visible');
     TorF = false;
     }else{
     $('.PassWord').next().css('visibility','hidden');
     TorF = true;
     }
     if(TorF == true){
     return true
     }else{
     return false
     }
     });*/
    /*$('.PassWord').focus(function () {
     if($('.EMail').val() == ''){
     $('.EMail').focus();
     $(this).blur();
     }
     })*/
    //  当图片不存在时加载默认图片
    $('.li-img').error(function () {
        $(this).attr('src','images/li4.png');
        $(this).onerror = null;  //避免当默认图片也不存在时陷入死循环
    });
    //  ORDER 页面的删除功能
    $('.list-table').on('click','.cou-detele',function () {
        var data  = $(this).parents('.coupons-center').attr('data-id');
        $('.Delete-Fram').css('display','table');
        var _this = this;
        $('.Yes-Btn').unbind().click(function () {
            $(this).parents('.Delete-Fram').css('display','none');
            $(_this).parents('.coupons-center').remove();
            alert(data);
        });
    });
    //  点击眼睛改变input中的状态
    $('.input-type').click(function () {
        var inputType = $(this).prev().prop('type');
        if(inputType == 'password'){
            $(this).prev().prop('type','text');
            $(this).css('color','#d6d48e');
        }else{
            $(this).prev().prop('type','password');
            $(this).css('color','#dbab6c');
        }
    });
    //  点击回复按钮回复框出现
    $('.message-reply').click(function () {
        $(this).next().css('display','block');
    })
});